package net.zjitc.list;

import net.zjitc.link.Node;

public class MyArrayList<T> implements MyList<T> {
    private Object[] arr = new Object[10];
    private int index = 0;
//    private int size;
    @Override
    public boolean add(T data) {
        Capacity();
        arr[index++] = data;
    return true;
    }



    @Override
    public boolean add(int index, T data) {
        return false;
    }

    @Override
    public T remove(T data) {
      T oldData = null;
      for (int i = 0;i<this.index;i++){
          if (arr[i].equals(data)){
              oldData = (T)arr[i];
              for (int j=i;j<this.index-1;j++){
                  arr[j] = arr[j+1];
              }
          }
      }
        return oldData;
    }

    @Override
    public T remove(int index) {
        return null;
    }

    @Override
    public boolean set(int index, T data) {
        if(index <0 || index>this.index){
            throw  new RuntimeException("超出索引");
        }
        arr[index] = data;
        return true;
    }

    @Override
    public T getValue(int index) {
        if(index <0 || index>this.index) {
            throw new RuntimeException("超出索引");
        }
        return (T)arr[index];
    }

    @Override
    public int size() {
        return size();
    }
    private void Capacity() {
        if (index>=arr.length){
            Object[] temp = new Object[arr.length*2];
            for (int i=0;i<arr.length;i++){
                temp[i] = arr[i];
            }
            arr =temp;
        }
    }

}
